package org.magento.pages;

import org.magento.data.AdminData;
import org.magento.utils.ConfigProperties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
//This class presents /admin page
public class LoginPageBeckEnd extends Page {

	//Locators:
	@FindBy(id = "username")
	public WebElement fieldUsername;
	
	@FindBy(id = "login")
	public WebElement fieldPassword;
	
	@FindBy(className ="form-button")
	public WebElement buttonLogin;
	
	@FindBy(linkText = "Forgot your password?")
	public WebElement forgotPasswordLink;

	
	//Actions 
	public LoginPageBeckEnd clickLoginButton() {
		buttonLogin.click();
		return PageFactory.initElements(driver, LoginPageBeckEnd.class);
	}	
	
	public ForgotAdminPassword gotoForgotPasswordPage() {
		forgotPasswordLink.click();
		return PageFactory.initElements(driver, ForgotAdminPassword.class);
	}
	
	public Dashboard loginAs (AdminData admin) {
		type(fieldUsername, admin.name);
		type(fieldPassword, admin.password);
		buttonLogin.click();
		return PageFactory.initElements(driver, Dashboard.class);
	}
	
	
	public LoginPageBeckEnd loginAsNot(AdminData admin) {
		type(fieldUsername, admin.name);
		type(fieldPassword, admin.name);
		buttonLogin.click();
		return PageFactory.initElements(driver, LoginPageBeckEnd.class);
		
	}
	
	//Assertions: 
	public boolean isPasswordReset() {
		 return  driver.findElement(By.cssSelector("BODY")).getText()
		.contains("If there is an account associated with denis.ulyanov@iceshop.nl you will receive an email with a link to reset your password.");
	}
	
	public boolean isloggedOut () {
	 return isElementPresent(buttonLogin);
	}
	
	public boolean isNotLoggedIn () {
			return isElementPresent(buttonLogin);
	}
	
	public boolean errorMessageisPresent() { 
		   return  driver.findElement(By.cssSelector("BODY")).getText().contains("Invalid User Name or Password");
	}
	
	public boolean isHintDisplayed() {
			return driver.findElement(By.cssSelector("BODY")).getText().contains("This is a required field.");
	}
		
	
	//Auto - generated
	public LoginPageBeckEnd(WebDriver driver) {
		super(driver);
	}

	@Override
	public void open() {
		driver.get(ConfigProperties.getProperty("url.be"));	
	}


	

}
